Page({
  data: {
    time: '12:30',
    currentDate: '2024-03',
    currentDateDisplay: '2024年3月',
    currentPeriod: 'month',
    financeSummary: {
      income: '286,450',
      expense: '96,280',
      profit: '190,170'
    },
    incomeTypes: ['按业务类型', '按门店', '按时间段'],
    incomeTypeIndex: 0,
    expenseTypes: ['按支出类型', '按门店', '按时间段'],
    expenseTypeIndex: 0,
    transactions: [
      {
        id: 1,
        title: '李先生婚纱租赁',
        date: '03-28',
        amount: '8,800',
        type: 'income',
        icon: 'icon-shopping-bag'
      },
      {
        id: 2,
        title: '王小姐定制礼服',
        date: '03-27',
        amount: '15,600',
        type: 'income',
        icon: 'icon-shopping-bag'
      },
      {
        id: 3,
        title: '面料采购',
        date: '03-26',
        amount: '12,300',
        type: 'expense',
        icon: 'icon-shopping'
      },
      {
        id: 4,
        title: '赵女士婚纱摄影',
        date: '03-25',
        amount: '6,800',
        type: 'income',
        icon: 'icon-camera'
      },
      {
        id: 5,
        title: '水电费缴纳',
        date: '03-25',
        amount: '3,480',
        type: 'expense',
        icon: 'icon-zap'
      }
    ]
  },

  onLoad: function() {
    // 初始化时间
    this.updateTime();
    this.getTimerID = setInterval(this.updateTime, 60000);
    
    // 加载财务数据
    this.loadFinanceData();
  },
  
  onUnload: function() {
    clearInterval(this.getTimerID);
  },
  
  updateTime: function() {
    const date = new Date();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    this.setData({
      time: `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`
    });
  },
  
  loadFinanceData: function() {
    // 这里可以从API获取财务数据
    // 示例代码:
    /*
    wx.request({
      url: 'https://api.example.com/finance/summary',
      method: 'GET',
      data: {
        date: this.data.currentDate,
        period: this.data.currentPeriod
      },
      success: (res) => {
        this.setData({
          financeSummary: res.data.summary,
          transactions: res.data.transactions
        });
      },
      fail: (err) => {
        console.error('获取财务数据失败', err);
        wx.showToast({
          title: '获取数据失败',
          icon: 'none'
        });
      }
    });
    */
  },
  
  onDateChange: function(e) {
    const date = new Date(e.detail.value);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    
    this.setData({
      currentDate: e.detail.value,
      currentDateDisplay: `${year}年${month}月`
    });
    
    // 更新数据
    this.loadFinanceData();
  },
  
  switchPeriod: function(e) {
    const period = e.currentTarget.dataset.period;
    
    this.setData({
      currentPeriod: period
    });
    
    // 更新财务数据摘要
    let summary = {};
    
    switch(period) {
      case 'month':
        summary = {
          income: '286,450',
          expense: '96,280',
          profit: '190,170'
        };
        break;
      case 'quarter':
        summary = {
          income: '859,350',
          expense: '288,840',
          profit: '570,510'
        };
        break;
      case 'year':
        summary = {
          income: '3,437,400',
          expense: '1,155,360',
          profit: '2,282,040'
        };
        break;
    }
    
    this.setData({
      financeSummary: summary
    });
  },
  
  onIncomeTypeChange: function(e) {
    this.setData({
      incomeTypeIndex: e.detail.value
    });
    // 这里可以加载或更新收入分析数据
  },
  
  onExpenseTypeChange: function(e) {
    this.setData({
      expenseTypeIndex: e.detail.value
    });
    // 这里可以加载或更新支出分析数据
  },
  
  viewAllTransactions: function() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },
  
  showReportOptions: function() {
    wx.showActionSheet({
      itemList: ['导出Excel报表', '导出PDF报表', '打印财务报表'],
      success: (res) => {
        wx.showToast({
          title: '功能开发中',
          icon: 'none'
        });
      }
    });
  }
}); 